package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.springdinner.BallData;
import com.yanqu.road.entity.activity.springdinner.ClientBallData;
import com.yanqu.road.entity.activity.springdinner.ComposeData;
import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SpringDinnerPb {

	public static int parseReqBallData(Map<Integer, BallData> ballMap, List<SpringDinnerProto.SpringDinnerFruitData> fruitDataList){
		Map<Integer, BallData> newBallMap = new ConcurrentHashMap<>();
		for(SpringDinnerProto.SpringDinnerFruitData ballMsg : fruitDataList){
			if(ballMsg.getPositionCount() != 2){
				return GameErrorCode.E_SPRINGDINNER_GAME_BALL_ERR;
			}
			BallData ballData = new BallData();
			ballData.setNo(ballMsg.getFruitId());
			ballData.setBallId(ballMsg.getLevel());

			ballData.setX(Double.parseDouble(ballMsg.getPosition(0)));
			ballData.setY(Double.parseDouble(ballMsg.getPosition(1)));
			newBallMap.put(ballData.getNo(),ballData);
		}
		ballMap.putAll(newBallMap);
		return 0;
	}

	public static int parseReqComposeData(Map<Integer, ComposeData> composeMap, List<SpringDinnerProto.SpringDinnerComposeData> composeDataList) {
		Map<Integer, ComposeData> newComposeMap = new ConcurrentHashMap<>();
		for(int i = 0;i < composeDataList.size() ; i++) {
			SpringDinnerProto.SpringDinnerComposeData composeMsg = composeDataList.get(i);
			if(composeMsg.getFruitData().getPositionCount() != 2){
				return GameErrorCode.E_SPRINGDINNER_GAME_BALL_ERR;
			}
			ComposeData composeData = new ComposeData();
			composeData.setBallIdA(composeMsg.getFruitIdA());
			composeData.setBallIdB(composeMsg.getFruitIdB());
			BallData ballData = new BallData();
			ballData.setNo(composeMsg.getFruitData().getFruitId());
			ballData.setBallId(composeMsg.getFruitData().getLevel());

			ballData.setX(Double.parseDouble(composeMsg.getFruitData().getPosition(0)));
			ballData.setY(Double.parseDouble(composeMsg.getFruitData().getPosition(1)));
			composeData.setBall(ballData);

			Map<Integer, ClientBallData> inComMap = new ConcurrentHashMap<>();
			for(SpringDinnerProto.SpringDinnerInComData inComData : composeMsg.getInComDataList()){
				if(inComData.getPositionCount() != 2 || inComData.getLinearVelocityCount() != 2){
					return GameErrorCode.E_SPRINGDINNER_GAME_BALL_ERR;
				}
				ClientBallData clientBallData = new ClientBallData();
				clientBallData.setNo(inComData.getFruitId());
				clientBallData.setScale(Double.parseDouble(inComData.getScale()));
				clientBallData.setX(Double.parseDouble(inComData.getPosition(0)));
				clientBallData.setY(Double.parseDouble(inComData.getPosition(1)));
				clientBallData.setxLinearVelocity(Double.parseDouble(inComData.getLinearVelocity(0)));
				clientBallData.setyLinearVelocity(Double.parseDouble(inComData.getLinearVelocity(1)));
				inComMap.put(clientBallData.getNo(),clientBallData);
			}
			composeData.setInComMap(inComMap);

			newComposeMap.put(i,composeData);
		}
		composeMap.putAll(newComposeMap);
		return 0;
	}



}
